package src.main.kotlin.day91

/**
 * 多泛型函数
 *
 */

fun main() {

    val box1:MagicBox3<boy3> = MagicBox3(boy3("jack", 20))
    val box2:MagicBox3<Dog3> = MagicBox3(Dog3(30))

    box1.available = true
    box1.fetch()?.run {
        println("you find a ${this.name}")
    }

    val man = box1.fetch {
        Man("jack", it.age.plus(15))

    }

    println(man)
}

class MagicBox3<T>(item:T){
    var available = false
    private var subject:T = item
    fun fetch(): T?{
        return subject.takeIf { available  }
    }

    //TODO: 业务： 把元素进行修改
    //TODO: 容器中放了boy3，取出来的时候，修改成了其他
    fun <R>fetch(subjectModFunction:(T)->R):R?{
        return subjectModFunction(subject).takeIf { available }
    }
}

class boy3(val name: String,val age:Int)
class Man(val name: String,val age:Int)


class Dog3(val weight:Int)

